package com.juyu.miliao.api.service;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.mysql.ActiveGiftDao;
import com.juyu.miliao.api.dao.mysql.GiftDao;
import com.juyu.miliao.api.domain.ActiveGift;
import com.juyu.miliao.api.domain.ActiveGiftRanking;
import com.juyu.miliao.api.domain.Gift;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.List;
import java.util.Optional;

@Service
public class ActiveGiftService {
    @Resource
    private ActiveGiftDao activeGiftDao;
    @Resource
    private GiftDao giftDao;

    public JSONObject getActiveGiftSwitchInfo() {
        JSONObject json = new JSONObject();
        long nowEpoch = Instant.now().toEpochMilli(); // 获取当前时间戳(毫秒)
        long homeStarEpoch = LocalDateTime.now().withHour(19).withMinute(30).withSecond(0)
                .toInstant(ZoneOffset.of("+8")).toEpochMilli();
        long homeEndEpoch = LocalDateTime.now().withHour(0).withMinute(30).withSecond(0)
                .toInstant(ZoneOffset.of("+8")).getEpochSecond();
        String activeDate = LocalDate.now().toString(); // 获取当前年月日
        ActiveGift activeGift = activeGiftDao.getActiveGiftSwitchInfo(activeDate);
        Optional<ActiveGift> optActiveGift = Optional.ofNullable(activeGift);
        if(!optActiveGift.isPresent()){
            return json;
        }
        json.put("homeIcon",activeGift.getHomeIcon());
        json.put("chatIcon",activeGift.getChatIcon());
        json.put("linkUrlHost",activeGift.getLinkUrlHost());
        json.put("linkUrlUser",activeGift.getLinkUrlUser());
        if(nowEpoch >= homeStarEpoch || nowEpoch<= homeEndEpoch){
            json.put("isShowHome",1);
            json.put("isShowChat",1);
        }else{
            json.put("isShowHome",2);
            json.put("isShowChat",2);
        }
        return json;
    }

    public JSONObject getActiveGiftInfo(int type) {
        JSONObject json = new JSONObject();
        long nowEpoch = Instant.now().getEpochSecond(); // 获取当前时间戳(秒)
        if(type ==1){ //今日榜单
            String activeDate = LocalDate.now().toString(); // 获取当前年月日
            ActiveGift activeGift = activeGiftDao.getActiveGiftSwitchInfo(activeDate);
            Optional<ActiveGift> optActiveGift = Optional.ofNullable(activeGift);
            if(!optActiveGift.isPresent()){
                return json;
            }
            Gift gift = giftDao.getGiftById(activeGift.getGiftId());

            long activeStarEpoch = LocalDateTime.now().withHour(20).withMinute(0).withSecond(0)
                    .toInstant(ZoneOffset.of("+8")).getEpochSecond();
            long activeEndEpoch = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0)
                    .toInstant(ZoneOffset.of("+8")).getEpochSecond();

            if(nowEpoch < activeStarEpoch){//未开始
                json.put("giftIcon",gift.getIcon());
                json.put("isActive",0);
                json.put("activeTime",activeStarEpoch-nowEpoch);
                json.put("giftNum",activeGift.getGiftNum());
                json.put("giftName",gift.getGiftName());
            }else{//进行中
                json.put("giftIcon",gift.getIcon());
                json.put("isActive",1);
                json.put("activeTime",activeEndEpoch-nowEpoch);
                json.put("giftNum",activeGift.getGiftNum());
                json.put("giftName",gift.getGiftName());
            }
        }else{ // 昨日榜
            String activeDate = LocalDate.now().minusDays(1).toString();
            ActiveGift activeGift = activeGiftDao.getActiveGiftSwitchInfo(activeDate);
            Optional<ActiveGift> optActiveGift = Optional.ofNullable(activeGift);
            if(!optActiveGift.isPresent()){
                return json;
            }
            Gift gift = giftDao.getGiftById(activeGift.getGiftId());
            json.put("giftIcon",gift.getIcon());
            json.put("isActive",2);
            json.put("activeTime",0);
            json.put("giftNum",activeGift.getGiftNum());
            json.put("giftName",gift.getGiftName());
        }
        return json;
    }

    public JSONObject getActiveGiftList(long userId, int type) {
        JSONObject json = new JSONObject();
        String activeDate = LocalDate.now().toString(); // 获取当前年月日
        if(type != 1) {
            activeDate = LocalDate.now().minusDays(1).toString();
        }
        ActiveGift activeGift = activeGiftDao.getActiveGiftSwitchInfo(activeDate);
        Optional<ActiveGift> optActiveGift = Optional.ofNullable(activeGift);
        if(!optActiveGift.isPresent()){
            return json;
        }
        List<ActiveGiftRanking> activeGiftRankingList = activeGiftDao.getActiveGiftList(activeDate);
        json.put("list",activeGiftRankingList);
        ActiveGiftRanking activeGiftRanking = new ActiveGiftRanking();
        int ranking = 0;
        for(int i=0;i<activeGiftRankingList.size();i++){
            if(activeGiftRankingList.get(i).getUserId()==userId){
                activeGiftRanking = activeGiftRankingList.get(i);
                ranking = i+1;
                break;
            }
        }
        Gift gift = giftDao.getGiftById(activeGift.getGiftId());
        json.put("giftIcon",gift.getIcon());
        json.put("giftName",gift.getGiftName());
        json.put("userRanking",ranking);
        json.put("userActiveGiftInfo",activeGiftRanking);
        return json;
    }
}
